<?php

namespace Ipartek\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Date;

/**
 * Noticia
 * @return Ipartek\BlogBundle\Entity\Noticia
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="noticia")
 * @ORM\Entity(repositoryClass="Ipartek\BlogBundle\Entity\NoticiaRepository")
 */
class Noticia
{
	
	
	/******************* RELACION con CATEGORIAS *************************************/
	
	/**
	 * @ORM\ManyToOne(targetEntity="Categoria", inversedBy="noticias")
	 * @ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
	 */
	protected $categoria;
	
	
	
	/******************* CONTRUCTOR *************************************/
	
	/**
	 * Constructo con parametros
	 * 
	 * @param unknown $_title    titulo
	 * @param unknown $_excerpt  resumen noticia 
	 * @param unknown $_content  contenido noticia
	 * @return Ipartek\BlogBundle\Entity\Noticia
	 */
	 
	public function __construct( $_title="sin titulo", $_tipo=0, $_excerpt="sin excerpt", $_content="sin content"){
		$this->title=$_title;
		$this->tipo=$_tipo;
		$this->slug= $this->slugify($_title);
		$this->excerpt=$_excerpt;
		$this->content=$_content;		
	}
	
	
	/******************* ATRIBUTTES *************************************/
	
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * 
     * @Assert\NotBlank
     * @Assert\NotNull
     * @Assert\MinLength(10)
     *
     * @ORM\Column(name="title", type="string", length=255, unique=true)
     */
    
    /**
     * @var integer
     * Tipo de Noticia: 0:'privado'  1:'publico' 
     * 
     * @ORM\Column(name="tipo",type="integer")
     * 
     */
    protected $tipo;
    
    
    /**
     * @ORM\Column(type="datetime")
     */
    protected $created_at;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $modified_at;
    
    
    
    
    /**
     * @var string
     *
     *
     * @ORM\Column(name="title", type="string", length=255, unique=true)
     */
    
    private $title;

    /**
     * @var string
     *     
     * 
     * @ORM\Column(name="slug", type="string", length=255, unique=true)
     */
    private $slug;

    /**
     * @var string
     *
     * @ORM\Column(name="excerpt", type="text" )
     */
    private $excerpt;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text")
     */
    private $content;

    
    /******************* GETTER/SETTERS *************************************/
    

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Noticia
     */
    public function setTitle($title)
    {
        $this->title = $title;
		$this->slug = $this->slugify($title);
        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    
    /**
     * Set tipo
     *
     * @param integer $tipo
     * @return Noticia
     */
    public function setTipo($tipo)
    {
    	$this->tipo = $tipo;
    
    	return $this;
    }
    
    /**
     * Get tipo
     *
     * @return integer
     */
    public function getTipo()
    {
    	return $this->tipo;
    }
    
    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return Noticia
     */
    public function setCreatedAt($createdAt)
    {
    	$this->created_at = $createdAt;
    
    	return $this;
    }
    
    /**
     * Get created_at
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
    	return $this->created_at;
    }
    
    /**
     * Set modified_at
     *
     * @param \DateTime $modifiedAt
     * @return Noticia
     */
    public function setModifiedAt($modifiedAt)
    {
    	$this->modified_at = $modifiedAt;
    
    	return $this;
    }
    
    /**
     * Get modified_at
     *
     * @return \DateTime
     */
    public function getModifiedAt()
    {
    	return $this->modified_at;
    }
    
    /**
     * Set slug
     *
     * @param string $slug
     * @return Noticia
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;
    
        return $this;
    }

    /**
     * Get slug
     *
     * @return string 
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set excerpt
     *
     * @param string $excerpt
     * @return Noticia
     */
    public function setExcerpt($excerpt)
    {
        $this->excerpt = $excerpt;
    
        return $this;
    }

    /**
     * Get excerpt
     *
     * @return string 
     */
    public function getExcerpt()
    {
        return $this->excerpt;
    }

    /**
     * Set content
     *
     * @param string $content
     * @return Noticia
     */
    public function setContent($content)
    {
        $this->content = $content;
    
        return $this;
    }

    /**
     * Get content
     *
     * @return string 
     */
    public function getContent()
    {
        return $this->content;
    }
    
    
    
    
    
    /**
     * Modifies a string to remove all non ASCII characters and spaces.
     */
    static private function slugify($text)
    {
    	// replace non letter or digits by -
    	$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
    
    	// trim
    	$text = trim($text, '-');
    
    	// transliterate
    	if (function_exists('iconv'))
    	{
    		$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    	}
    
    	// lowercase
    	$text = strtolower($text);
    
    	// remove unwanted characters
    	$text = preg_replace('~[^-\w]+~', '', $text);
    
    	if (empty($text))
    	{
    		return 'n-a';
    	}
    
    	return $text;
    }
    
    
    
    
    /*****************  RELACION 1-N con CATEGORIAS **************************************/

    /**
     * Set categoria
     *
     * @param \Ipartek\BlogBundle\Entity\Categoria $categoria
     * @return Noticia
     */
    public function setCategoria(\Ipartek\BlogBundle\Entity\Categoria $categoria = null)
    {
        
   		if ( $categoria == null){
   			$this->categoria = new Categoria();
   		}else{
    		$this->categoria = $categoria;
   		}	
    
        return $this;
    }

    /**
     * Get categoria
     *
     * @return \Ipartek\BlogBundle\Entity\Categoria 
     */
    public function getCategoria()
    {
        return $this->categoria;
    }
    
    
    
    /*****************  CALLBACKS **************************************/
    
    
    /**
     * Now we tell doctrine that before we persist or update we call the updatedTimestamps() function.
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps()
    {
    	$this->setModifiedAt(new \DateTime(date('Y-m-d H:i:s')));
    
    	if($this->getCreatedAt() == null)
    	{
    		$this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
    	}
    }
    
    

  
}